var controller = {
	/**
	 * 初始化
	 */
	init : function() {
		var _this = this;
		_this.bindClick();
	},
	/**
	 * 绑定事件
	 */
	bindEvent : function() {
		var _this = this;
		_this.bindClick()
	},
	bindClick : function() {

		$(".menu_selected").bind("click", function() {
			// 资源属性
			var authority_id = $(this).val();
			$("#editMenu").val(authority_id);
			ajax.loadResource(authority_id);
		});

		$("#editMenu").bind("click", function() {
			$("#addResourceDialog").modal("hide");
			// 资源属性
			var authority_id = $(this).val();
			var mycars = new Array();
			var i = 0;
			$("input[name='resource_enabled']:checked").each(function() {
				var authorityResource = {
					authority_id : authority_id,
					resource_id : $(this).val(),
					enabled : 1
				}
				mycars[i] = authorityResource;
				i = i + 1;
			});
			if (mycars.length == 0) {
				App.myDialog("请至少选择一个菜单！", "提示");
				return;
			}
			debugger;
			var resourceString = JSON.stringify(mycars);
			ajax.editMenu(resourceString);
		});

		$("#addDialogBtn").bind("click", function() {
			$("#editAuthorityBtn").addClass("hide");
			$("#addAuthorityBtn").removeClass("hide");
			$("#addDialog").modal('show');
		});

		$("#addAuthorityBtn").bind(
				"click",
				function() {
					// 资源属性
					var authority_name = $("#authorityName").val();
					var authority_desc = $("#authorityDesc").val();
					var enabled = $("input[name='enabled']:checked").val();
					var issys = $("input[name='issys']:checked").val();
					if (enabled == 'on') {
						enabled = 1;
					} else {
						enabled = 0;
					}
					if (issys == 'on') {
						issys = 1;
					} else {
						issys = 0;
					}

					var authority = {
						authority_name : authority_name,
						authority_desc : authority_desc,
						enabled : enabled,
						issys : issys
					}
					var authorityString = JSON.stringify(authority);
					ajax.operationAuthority("/authority/add", authorityString,
							"添加权限信息成功！", "添加权限信息失败：");
				});
		
		$(".remove_authority").bind("click", function() {
			// 资源属性
			var authority_id = $(this).val();
			ajax.deleteAuthority("/authority/delete?authorityId=" + authority_id);
		});
		
		$(".edit_authority").bind("click", function() {
			// 资源属性
			var authority_id = $(this).val();
			$("#editAuthorityBtn").removeClass("hide");
			$("#addAuthorityBtn").addClass("hide");
			var authority = ajax.load("/authority?authorityId=" + authority_id);
			$("#authorityId").val(authority.authority_id);
			$("#authorityName").val(authority.authority_name);
			$("#authorityDesc").val(authority.authority_desc);
			debugger
			if(authority.enabled == 1) {
				$("#enabled").parent().attr("aria-checked","true");
				$("#enabled").parent().addClass("checked");
				$("#enabled").attr("checked","checked");
			}
			if(authority.issys == 1) {
				$("#issys").parent().attr("aria-checked","true");
				$("#issys").parent().addClass("checked");
				$("#enabled").attr("checked","checked");
			}
			//###################
			$("#addDialog").modal('show');
		});
		
		$("#editResourceBtn").bind("click", function() {
			// 资源属性
			var authority_id = $("#authorityId").val();
			var authority_name = $("#authorityName").val();
			var authority_desc = $("#authorityDesc").val();
			var enabled = $("input[name='enabled']:checked").val();
			var issys = $("input[name='issys']:checked").val();
			if (enabled == 'on') {
				enabled = 1;
			} else {
				enabled = 0;
			}
			if (issys == 'on') {
				issys = 1;
			} else {
				issys = 0;
			}

			var authority = {
				authority_id : authority_id,
				authority_name : authority_name,
				authority_desc : authority_desc,
				enabled : enabled,
				issys : issys
			}
			var authorityString = JSON.stringify(authority);
			ajax.operationAuthority("/authority/edit", authorityString,
					"修改权限信息成功！", "修改权限信息失败：");
		});
	}
};
var ajax = {
	loadResource : function(data) {
		$
				.ajax({
					url : "/resource/authority/list?authorityId=" + data,
					type : "GET",
					async : false,
					data : data,
					dataType : "json",
					contentType : 'application/json;charset=utf-8',
					success : function(rs) {
						if (rs.success) {
							$("#addResourceDialog").modal("show");
							$("#menu_table").html("");
							$("#menu_table")
									.append(
											"<tr><th style='width: 60px'>操作</th><th>菜单名称</th><th>菜单地址</th></tr>");
							for (var i = 0; i < rs.result.length; i++) {
								var trString = "<tr><td><label><input name='resource_enabled' type='checkbox' value='"
										+ rs.result[i].resource_id + "'";
								if (rs.result[i].ifInuse) {
									trString = trString + " checked";

								}
								trString = trString + "></label></td><td>"
										+ rs.result[i].resource_desc
										+ "</td><td>"
										+ rs.result[i].resource_String
										+ "</td></tr>"

								$("#menu_table").append(trString);
							}
							$("input[type='checkbox']").iCheck({
								checkboxClass : 'icheckbox_minimal'
							});
						} else {
							App.myDialog("添加菜单失败：" + rs.result, "提示");
						}
					}
				});
	},
	editMenu : function(data) {
		$.ajax({
			url : "/resource/authority/edit",
			type : "POST",
			async : false,
			data : data,
			dataType : "json",
			contentType : 'application/json;charset=utf-8',
			success : function(rs) {
				if (rs.success) {
					App.myDialog("修改菜单权限成功！", "提示");
				} else {
					App.myDialog("修改菜单权限失败：" + rs.result, "提示");
				}
			}
		});
	},
	operationAuthority : function(url, data, successTips, errorTips) {
		$.ajax({
			url : url,
			type : "POST",
			async : false,
			dataType : "json",
			data : data,
			contentType : 'application/json;charset=utf-8',
			success : function(rs) {
				$("#addDialog").modal('hide')
				if (rs.success) {
					App.myDialog(successTips, "提示");
				} else {
					App.myDialog(errorTips + rs.result, "提示");
				}
			}
		});
	},
	deleteAuthority : function(url) {
		$.ajax({
			url : url,
			type : "GET",
			async : false,
			dataType : "json",
			contentType : 'application/json;charset=utf-8',
			success : function(rs) {
				if (rs.success) {
					App.myDialog("删除权限成功！", "提示");
				} else {
					App.myDialog("删除权限失败：" + rs.result, "提示");
				}
			}
		});
	},
	load : function(url) {
		var result;
		$.ajax({
			url : url,
			type : "GET",
			async : false,
			dataType : "json",
			contentType : 'application/json;charset=utf-8',
			success : function(rs) {
				if (rs.success) {
					result = rs.result;
				} else {
					App.myDialog("获取权限详情失败：" + rs.resultMessage, "提示");
				}
			}
		});
		
		return result;
	}
};
$(function() {
	controller.init();
});